package du.myseckill.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabbitMQConfig {

    //交换机
    //秒杀队列交换机
    @Bean
    public FanoutExchange secKillFanoutExchange() {
        return new FanoutExchange("secKill_fanout_exchange");
    }

    //秒杀死信队列交换机
    @Bean
    public FanoutExchange secKillDeadFanoutExchange() {
        return new FanoutExchange("secKill_dead_fanout_exchange");
    }

    //队列
    //秒杀队列
    @Bean
    public Queue secKillQueue() {
        HashMap<String, Object> map = new HashMap<>();
        //指定死信队列
        map.put("x-dead-letter-exchange", "secKill_dead_fanout_exchange");
        //设置秒杀消息的过期时间为2秒
        map.put("x-message-ttl", 2000);
        return new Queue("secKill_queue", true, false, false, map);
    }

    //秒杀死信队列
    @Bean
    public Queue secKillDeadQueue() {
        return new Queue("secKill_dead_queue", true, false, false);
    }

    //绑定
    //秒杀
    @Bean
    public Binding secKillBinding() {
        return BindingBuilder.bind(secKillQueue()).to(secKillFanoutExchange());
    }

    //秒杀死信
    @Bean
    public Binding secKillDeadBinding() {
        return BindingBuilder.bind(secKillDeadQueue()).to(secKillDeadFanoutExchange());
    }

}
